<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="正则表达式正则表达式是对字符串操作的一种逻辑公式，就是用事先定义好的一些特定字符、及这些特定字符的组合，组成一个“规则字符串”，这个“规则字符串”用来表达对字符串的一种过滤逻辑">
<meta property="og:type" content="article">
<meta property="og:title" content="正则表达式">
<meta property="og:url" content="http://yoursite.com/2018/07/20/正则表达式/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="正则表达式正则表达式是对字符串操作的一种逻辑公式，就是用事先定义好的一些特定字符、及这些特定字符的组合，组成一个“规则字符串”，这个“规则字符串”用来表达对字符串的一种过滤逻辑">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-07-19T05:40:06.061Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="正则表达式">
<meta name="twitter:description" content="正则表达式正则表达式是对字符串操作的一种逻辑公式，就是用事先定义好的一些特定字符、及这些特定字符的组合，组成一个“规则字符串”，这个“规则字符串”用来表达对字符串的一种过滤逻辑">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/07/20/正则表达式/"/>





  <title>正则表达式 | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/07/20/正则表达式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">正则表达式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-07-20T09:52:56+08:00">
                2018-07-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/正则表达式/" itemprop="url" rel="index">
                    <span itemprop="name">正则表达式</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/07/20/正则表达式/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/07/20/正则表达式/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h4><p><strong>正则表达式是对字符串操作的一种逻辑公式，就是用事先定义好的一些特定字符、及这些特定字符的组合，组成一个“规则字符串”，这个“规则字符串”用来表达对字符串的一种过滤逻辑</strong><a id="more"></a></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 匹配字符串中所有的复合规则的字符串,返回匹配到的范围 NSTextCheckingResult 里面有两个属性一个就是Range</span></span><br><span class="line"> <span class="keyword">open</span> <span class="function"><span class="keyword">func</span> <span class="title">matches</span><span class="params">(<span class="keyword">in</span> string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange)</span></span> -&gt; [<span class="type">NSTextCheckingResult</span>]</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 按照规则匹配字符串,返回匹配到的个数</span></span><br><span class="line"> <span class="keyword">open</span> <span class="function"><span class="keyword">func</span> <span class="title">numberOfMatches</span><span class="params">(<span class="keyword">in</span> string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange)</span></span> -&gt; <span class="type">Int</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 按照规则匹配字符串,返回第一个匹配到的字符串的范围</span></span><br><span class="line"> <span class="keyword">open</span> <span class="function"><span class="keyword">func</span> <span class="title">firstMatch</span><span class="params">(<span class="keyword">in</span> string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange)</span></span> -&gt; <span class="type">NSTextCheckingResult</span>?</span><br><span class="line"> <span class="comment">// 按照规则匹配字符串,返回第一个匹配到的字符串的范围,这个是直接返回Range</span></span><br><span class="line"> <span class="keyword">open</span> <span class="function"><span class="keyword">func</span> <span class="title">rangeOfFirstMatch</span><span class="params">(<span class="keyword">in</span> string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange)</span></span> -&gt; <span class="type">NSRange</span></span><br></pre></td></tr></table></figure>
<h5 id="方法一"><a href="#方法一" class="headerlink" title="方法一"></a>方法一</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"123456"</span></span><br><span class="line"><span class="comment">//  创建规则</span></span><br><span class="line"><span class="keyword">let</span> pattern = <span class="string">"[1-9][0-9]&#123;4,14&#125;"</span>        </span><br><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line"><span class="comment">//  创建正则表达式对象    caseInsensitive 忽略大小写</span></span><br><span class="line"><span class="keyword">let</span> regex = <span class="keyword">try</span> <span class="type">NSRegularExpression</span>(pattern: pattern, options: <span class="type">NSRegularExpression</span>.<span class="type">Options</span>.caseInsensitive)</span><br><span class="line"><span class="comment">//  开始匹配</span></span><br><span class="line"><span class="keyword">let</span> range = regex.rangeOfFirstMatch(<span class="keyword">in</span>: str, options: <span class="type">NSRegularExpression</span>.<span class="type">MatchingOptions</span> (rawValue: <span class="number">0</span>), range: <span class="type">NSRange</span> (location: <span class="number">0</span>, length: str.<span class="built_in">count</span>))</span><br><span class="line"><span class="built_in">print</span>(range)</span><br><span class="line">&#125;<span class="keyword">catch</span>&#123;</span><br><span class="line"><span class="built_in">print</span>(error)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="方法二"><a href="#方法二" class="headerlink" title="方法二"></a>方法二</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  前面加个a发现也会符合规则,是因为正则表达式会匹配整个字符串,前面不匹配往后匹配</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">"123456"</span>  </span><br><span class="line"><span class="comment">//  创建规则</span></span><br><span class="line"><span class="keyword">let</span> pattern = <span class="string">"[1-9][0-9]&#123;4,14&#125;"</span>    <span class="comment">// &#123;4,14&#125; 意思是从第二位开始算(也就是[0-9]是第二位的约束) ,最少4位最多14位</span></span><br><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line"><span class="comment">//  创建正则表达式对象    caseInsensitive 忽略大小写</span></span><br><span class="line"><span class="keyword">let</span> regex = <span class="keyword">try</span> <span class="type">NSRegularExpression</span>(pattern: pattern, options: <span class="type">NSRegularExpression</span>.<span class="type">Options</span>.caseInsensitive)</span><br><span class="line"><span class="comment">//  开始匹配</span></span><br><span class="line"><span class="keyword">let</span> number = regex.numberOfMatches(<span class="keyword">in</span>: str, options: <span class="type">NSRegularExpression</span>.<span class="type">MatchingOptions</span> (rawValue: <span class="number">0</span>), range: <span class="type">NSRange</span> (location: <span class="number">0</span>, length: str.<span class="built_in">count</span>))</span><br><span class="line"><span class="built_in">print</span>(number)   <span class="comment">// 打印1则符合规则,0不符合</span></span><br><span class="line">&#125;<span class="keyword">catch</span>&#123;</span><br><span class="line"><span class="built_in">print</span>(error)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="方法三"><a href="#方法三" class="headerlink" title="方法三"></a>方法三</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"123456"</span></span><br><span class="line"><span class="comment">//  创建规则</span></span><br><span class="line"><span class="keyword">let</span> pattern = <span class="string">"[1-9][0-9]&#123;4,14&#125;"</span></span><br><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line"><span class="comment">//  创建正则表达式对象    caseInsensitive 忽略大小写</span></span><br><span class="line"><span class="keyword">let</span> regex = <span class="keyword">try</span> <span class="type">NSRegularExpression</span>(pattern: pattern, options: <span class="type">NSRegularExpression</span>.<span class="type">Options</span>.caseInsensitive)</span><br><span class="line"><span class="comment">//  开始匹配</span></span><br><span class="line"><span class="keyword">let</span> result = regex.firstMatch(<span class="keyword">in</span>: str, options: <span class="type">NSRegularExpression</span>.<span class="type">MatchingOptions</span> (rawValue: <span class="number">0</span>), range: <span class="type">NSRange</span> (location: <span class="number">0</span>, length: str.<span class="built_in">count</span>))</span><br><span class="line"><span class="built_in">print</span>(result!.range)</span><br><span class="line">&#125;<span class="keyword">catch</span>&#123;</span><br><span class="line"><span class="built_in">print</span>(error)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="方法四"><a href="#方法四" class="headerlink" title="方法四"></a>方法四</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"123456"</span></span><br><span class="line"><span class="comment">//  创建规则</span></span><br><span class="line"><span class="keyword">let</span> pattern = <span class="string">"[1-9][0-9]&#123;4,14&#125;"</span>        </span><br><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line"><span class="comment">//  创建正则表达式对象    caseInsensitive 忽略大小写</span></span><br><span class="line"><span class="keyword">let</span> regex = <span class="keyword">try</span> <span class="type">NSRegularExpression</span>(pattern: pattern, options: <span class="type">NSRegularExpression</span>.<span class="type">Options</span>.caseInsensitive)</span><br><span class="line"><span class="comment">//  开始匹配</span></span><br><span class="line"><span class="keyword">let</span> range = regex.rangeOfFirstMatch(<span class="keyword">in</span>: str, options: <span class="type">NSRegularExpression</span>.<span class="type">MatchingOptions</span> (rawValue: <span class="number">0</span>), range: <span class="type">NSRange</span> (location: <span class="number">0</span>, length: str.<span class="built_in">count</span>))</span><br><span class="line"><span class="built_in">print</span>(range)</span><br><span class="line">&#125;<span class="keyword">catch</span>&#123;</span><br><span class="line"><span class="built_in">print</span>(error)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="当初表情过滤用的表达式"><a href="#当初表情过滤用的表达式" class="headerlink" title="当初表情过滤用的表达式"></a>当初表情过滤用的表达式</h5><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"1234[123]5[21]621a[1 ]"</span></span><br><span class="line"><span class="comment">//  创建规则 ^ 代表取反 比如[^a-z]意思是不是字母就符合</span></span><br><span class="line"><span class="keyword">let</span> pattern = <span class="string">"\\[[^\\[\\]\n]*\\]"</span></span><br><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line"><span class="comment">//  创建正则表达式对象    caseInsensitive 忽略大小写</span></span><br><span class="line"><span class="keyword">let</span> regex = <span class="keyword">try</span> <span class="type">NSRegularExpression</span>(pattern: pattern, options: <span class="type">NSRegularExpression</span>.<span class="type">Options</span>.caseInsensitive)</span><br><span class="line"><span class="comment">//  开始匹配</span></span><br><span class="line"><span class="keyword">let</span> checkingResult = regex.matches(<span class="keyword">in</span>: str, options: <span class="type">NSRegularExpression</span>.<span class="type">MatchingOptions</span>(rawValue: <span class="number">0</span>), range: <span class="type">NSRange</span> (location: <span class="number">0</span>, length: str.<span class="built_in">count</span>))</span><br><span class="line"><span class="keyword">for</span> result <span class="keyword">in</span> checkingResult &#123;</span><br><span class="line"><span class="built_in">print</span>(result.range)</span><br><span class="line">&#125;</span><br><span class="line">&#125;<span class="keyword">catch</span>&#123;</span><br><span class="line"><span class="built_in">print</span>(error)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="正则表达式-1"><a href="#正则表达式-1" class="headerlink" title="正则表达式"></a>正则表达式</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">正则表达式:</span><br><span class="line">在编写处理字符串的程序时，经常会有查找符合某些复杂规则的字符串的需要。正则表达式就是用于描述这些规则的工具。换句话说，正则表达式就是记录文本规则的代码</span><br><span class="line"></span><br><span class="line">正则表达式是对字符串操作的一种逻辑公式，用事先定义好的一些特定字符、及这些特定字符的组合，组成一个&quot;规则字符串&quot;，这个&quot;规则字符串&quot;用来表达对字符串的一种过滤逻辑。</span><br><span class="line"></span><br><span class="line">－几乎所有的程序设计语言都支持正则表达式，例如：OC，java，c#，python，js等</span><br><span class="line">－在很多文本编辑器里，可以使用正则表达式进行检索，Xcode同样支持正则表达式！</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">正则表达式的用处:</span><br><span class="line">1. 判断给定的字符串是否符合某一种规则(专门用于操作字符串)</span><br><span class="line">    &gt; 电话号码，电子邮箱，URL...</span><br><span class="line">    可以直接百度别人写好的正则</span><br><span class="line">    1&gt; 	别人真的写好了，而且测试过了，我们可以直接用</span><br><span class="line">    2&gt;	要写出没有漏洞正则判断，需要大量的测试，通常最终结果非常负责</span><br><span class="line"></span><br><span class="line">2. 过滤筛选字符串，网络爬虫</span><br><span class="line">3. 替换文字，QQ聊天，图文混排</span><br><span class="line"></span><br><span class="line">0&gt;	匹配</span><br><span class="line">--------------------------------------------------------------------------------</span><br><span class="line">(pattern)		匹配pattern并获取这一匹配，所获取的匹配可以从产生的Matches集合得到</span><br><span class="line"></span><br><span class="line">1&gt;	集合</span><br><span class="line">--------------------------------------------------------------------------------</span><br><span class="line">[xyz]		字符集合(x/y或z)</span><br><span class="line">[a-z]		字符范围</span><br><span class="line">[a-zA-Z]</span><br><span class="line">[^xyz]		负值字符集合 (任何字符, 除了xyz)</span><br><span class="line">[^a-z]		负值字符范围</span><br><span class="line">[a-d][m-p]  并集(a到d 或 m到p)</span><br><span class="line"></span><br><span class="line">2&gt;	常用元字符</span><br><span class="line">--------------------------------------------------------------------------------</span><br><span class="line">.			匹配除换行符以外的任意字符</span><br><span class="line">\w			匹配字母或数字或下划线或汉字 [a-zA-Z_0-9]</span><br><span class="line">\s			匹配任意的空白符（空格、TAB\t、回车\r \n）</span><br><span class="line">\d			匹配数字 [0-9]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">^			匹配字符串的开始</span><br><span class="line">$			匹配字符串的结束</span><br><span class="line">\b			匹配单词的开始或结束</span><br><span class="line"></span><br><span class="line">2&gt;	常用反义符</span><br><span class="line">--------------------------------------------------------------------------------</span><br><span class="line">\W          匹配任意不是字母，数字，下划线，汉字的字符[^\w]</span><br><span class="line">\S			匹配任意不是空白符的字符 [^\s]</span><br><span class="line">\D			匹配任意非数字的字符[^0-9]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">\B			匹配不是单词开头或结束的位置</span><br><span class="line">[^x]		匹配除了x以外的任意字符</span><br><span class="line">[^aeiou]	匹配除了aeiou这几个字母以外的任意字符</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">4&gt;	常用限定符</span><br><span class="line">--------------------------------------------------------------------------------</span><br><span class="line">*			重复零次或更多次</span><br><span class="line">+			重复一次或更多次</span><br><span class="line">?			重复零次或一次</span><br><span class="line">&#123;n&#125;			重复n次</span><br><span class="line">&#123;n,&#125;		重复n次或更多次</span><br><span class="line">&#123;n,m&#125;		重复n到m次,</span><br><span class="line"></span><br><span class="line">5&gt;	贪婪和懒惰</span><br><span class="line">--------------------------------------------------------------------------------</span><br><span class="line">*?			重复任意次，但尽可能少重复</span><br><span class="line">*+			重复1次或更多次，但尽可能少重复</span><br><span class="line">??			重复0次或1次，但尽可能少重复</span><br><span class="line">&#123;n,m&#125;?      重复n到m次，但尽可能少重复</span><br><span class="line">&#123;n,&#125;?		重复n次以上，但尽可能少重复</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/07/14/UIViewAnimation/" rel="next" title="UIViewAnimation">
                <i class="fa fa-chevron-left"></i> UIViewAnimation
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/07/21/私有库/" rel="prev" title="私有库">
                私有库 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#正则表达式"><span class="nav-number">1.</span> <span class="nav-text">正则表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#方法一"><span class="nav-number">1.1.</span> <span class="nav-text">方法一</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#方法二"><span class="nav-number">1.2.</span> <span class="nav-text">方法二</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#方法三"><span class="nav-number">1.3.</span> <span class="nav-text">方法三</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#方法四"><span class="nav-number">1.4.</span> <span class="nav-text">方法四</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#当初表情过滤用的表达式"><span class="nav-number">1.5.</span> <span class="nav-text">当初表情过滤用的表达式</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#正则表达式-1"><span class="nav-number">1.6.</span> <span class="nav-text">正则表达式</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
